package com.ziyun.pop.mall.module.cart.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.service.cart.api.CartService;
import com.service.cart.model.CartResult;
import com.service.cart.model.cart.AddCartDto;
import com.service.cart.model.cart.CartAppendParam;
import com.service.cart.model.cart.CartInfoVO;
import com.service.cart.model.cart.ChangeNumDto;
import com.service.common.constant.AppType;
import com.service.common.constant.CartType;
import com.utility.JsonUtils;
import com.ziyun.pop.mall.system.config.HttpRequestLocal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @Author:chenxiaoyi
 * @Date:2018/9/6 11:54
 */

@Controller
public class PopCartController {

	private static Logger _log = LoggerFactory.getLogger(PopCartController.class);
	private final String MODEL = "/cart";

	@Autowired
	private CartService cartService;

	@Autowired
	private HttpRequestLocal httpRequestLocal;

	// 加载购物车页面
	@GetMapping(MODEL+"/toLoadCart.html")
	public ModelAndView toCart() {
		ModelAndView view = new ModelAndView("cart/index.html");
		//CartInfoVO cartInfoVO = cartService.loadCart(CartType.all).getData();
		//view.addObject("cartInfoVo", cartInfoVO);
		return view;
	}

	@PostMapping(MODEL+"/toLoadCart.json")
	@ResponseBody
	public CartResult<CartInfoVO> toLoadCart(){
		return cartService.loadCart(CartType.all);
	}

	@PostMapping(MODEL+"/getCartInfo.json")
	@ResponseBody
	public CartResult<CartInfoVO> getCartInfo() {
		return cartService.loadCart(CartType.all);
	}

	// 商品加入购物车
	@PostMapping(MODEL+"/productAddCart")
	@ResponseBody
	public CartResult addItem(String addCartDto) {
		AddCartDto cartDto = JSON.parseObject(addCartDto, new TypeReference<AddCartDto>() {
		});
		return cartService.addItem(AppType.pc,null,cartDto);
	}

	/**
	 * 购物车商品全选或反选
	 *
	 */
	@PostMapping(MODEL+"/selectCartAll.json")
	@ResponseBody
	public CartResult<?> selectCartAll(String param) {
		Map<String, Object> map = JsonUtils.string2Map(param, String.class, Object.class);
		return cartService.selectAll(map);
	}

	/**
	 * 购物车商品单选
	 *
	 */
	@PostMapping(MODEL+"/selectCart.json")
	@ResponseBody
	public CartResult<?> selectCart(String param) {
		Map<String, String> map = JsonUtils.string2Map(param, String.class, String.class);
		return cartService.selectItem(map);
	}

	/**
	 * 购物车商品不选
	 *
	 */
	@PostMapping(MODEL+"/cancelCart.json")
	@ResponseBody
	public CartResult<?> cancelCart(String param) {
		Map<String, String> map = JsonUtils.string2Map(param, String.class, String.class);
		return cartService.cancelItem(map);
	}


	/**
	 * 删除购物车商品
	 *
	 */
	@PostMapping(MODEL+"/deleteItem.json")
	@ResponseBody
	public CartResult<?> deleteitem(String param) {
		Map<String, String> map = JsonUtils.string2Map(param, String.class, String.class);
		return cartService.delItem(map);
	}

	/**
	 * 改变数量
	 */
	@PostMapping(MODEL+"/changeNum.json")
	@ResponseBody
	public CartResult<?> changeNum(String param){
		ChangeNumDto dto = JsonUtils.string2Object(param, ChangeNumDto.class);
		return cartService.changeNum(dto);
	}

	/**
	 * 修改附件
	 */
	@PostMapping(MODEL+"/addAppend.json")
	@ResponseBody
	public CartResult<?> updateAppend(String param){
		CartAppendParam cartAppendParam = JsonUtils.string2Object(param, CartAppendParam.class);
		return cartService.addAppend(cartAppendParam);
	}

	/**
	 * 删除附件
	 */
	@PostMapping(MODEL+"/delAppend.json")
	@ResponseBody
	public CartResult<?> delAppend(String param){
		CartAppendParam appendParam = JsonUtils.string2Object(param, CartAppendParam.class);
		return cartService.delAppend(appendParam);
	}

	/**
	 * 查看当前购物车数量
	 */
	@PostMapping(MODEL+"/getCartCount.json")
	@ResponseBody
	public CartResult<Integer> getCartCount(){
		Long userId =  (Long)httpRequestLocal.getSessionValue("userId");
		CartResult<Integer> num = new CartResult<>();
		if(userId == null){
			num.setCode(999);
		}else{
			num = cartService.getCartNum();
		}
		return num;
	}

}
